package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwRefundCarService;
import com.zjdiepu.www.ibiz.icbc.IOtherLoanLossProvisionService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.icbc.dto.OtherLoanLossProvision;
import com.zjdiepu.www.model.icbc.ext.OtherLoanLossProvisionExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 退提车款Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 20时36分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "CwRefundCarViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"退提车款"}, produces="text/html;charset=UTF-8")
public class CwRefundCarViewController extends BaseController {
    @Resource
    private ICwRefundCarService cwRefundCarService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IOtherLoanLossProvisionService otherLoanLossProvisionservice;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: refundCarDetail <br/>
     * @Description: 退提车款详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "退提车款详情", response = Boolean.class, httpMethod="GET", tags={"退提车款"})
    @RequestMapping(value = "refundCarDetail", method = RequestMethod.GET)
    public String refundCarDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	model.addAttribute("loanBank", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.REMIT_TYPE,"请选择..."));
    	model.addAttribute("remitFinanceAuditor", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.FINANCE_AUDITOR,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null){
    		viewScopeControllerHelper.preparedYwFee(id, ywBusinessDetail.getCustomerId(), model);

    		if("ICBC".equals(ywBusinessDetail.getLoanBank())){
        		model.addAttribute("prepare_account", true);
        		
        		//风险准备金
        		OtherLoanLossProvisionExt otherLoanLossProvisionExtParam = new OtherLoanLossProvisionExt();
        		otherLoanLossProvisionExtParam.setBaseWhere();
        		otherLoanLossProvisionExtParam.withOutPage();
        		List<OtherLoanLossProvision> otherLoanLossProvisions = otherLoanLossProvisionservice.selectByExt(otherLoanLossProvisionExtParam);
        		model.addAttribute("otherLoanLossProvisions", otherLoanLossProvisions);
        	}
    	}
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id, ywBusinessDetail.getCustomerId(),model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    	}
    	viewScopeControllerHelper.preparedCarModel(model);
    	
    	//特殊订单
		YwBusinessExtras ywBusinessExtras = new YwBusinessExtras();
		ywBusinessExtras.setDpStatus(DpStatus.NORMAL.getCode());;
		ywBusinessExtras.setCodeExtras(BusinessExtrasCodeType.IS_SPECIAL_ORDER.getCode());
		ywBusinessExtras.setTypeExtras(BusinessExtrasType.AUDIT.getCode());
		ywBusinessExtras.setBusiId(id);
		YwBusinessExtras ywBusinessExtras_ = ywBusinessExtrasService.selectRefined(ywBusinessExtras);
		boolean isSpecialOrder = false;
		if(null != ywBusinessExtras_) {
			if("1".equals(ywBusinessExtras_.getValueExtras())) {
				isSpecialOrder = true;
//				return ApiResponse.fail(999,"该订单被风控标注为特殊订单");
			}
		}
		model.addAttribute("isSpecialOrder", isSpecialOrder);
		
    	String history = cwRefundCarService.queryHistoryByBusiId(id);
    	model.addAttribute("history", StringUtils.isBlank(history)?"-":(history.startsWith(",")?history.substring(1):history));
    	
    	SysUser sysUser =(SysUser)getCurrentUser();
    	viewScopeControllerHelper.preparedAllocation(sysUser.getOrgId(), model);
    	
    	return "system/cw/refundCarDetail";
    }
	
}
